package com.sx.pipeline.ui.advertise

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.LoginDTO
import com.sx.pipeline.repos.http.req.MessageDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: 广告宣传VM
 */
class AdvertisingVM(app: Application) : CYBaseViewModel(app) {

    // 广告宣传列表
    val advertisingListEvent: SingleLiveEvent<Ipage<AdvertiseVO>> = SingleLiveEvent()

    /**
     * 获取学习中心列表
     */
    fun getAdvertiseList(page: Int = 1) {
        addSubscribe(mModel.getAdvertiseList(isPage = true,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<AdvertiseVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<AdvertiseVO>) {
                        advertisingListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }


    /**
     * 获取商品列表
     */
    var page = 1
    val deviceListEvent: SingleLiveEvent<AdvertiseDetailVO> = SingleLiveEvent()
    fun getAdvertisingDetail(unitId: Int) {
        addSubscribe(
            mModel.getAdvertisingDetail(unitId, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<AdvertiseDetailVO, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: AdvertiseDetailVO) {
                        deviceListEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    val advertiseIntroEvent: SingleLiveEvent<AgreementVO> = SingleLiveEvent()
    fun getAdvertisingIntro(id: Int) {
        addSubscribe(mModel.getAdvertisingIntro(id)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<AgreementVO, BaseViewModel<*>>
                (this, false) {
                override fun onResult(result: AgreementVO) {
                    advertiseIntroEvent.postValue(result)
                }
            })
        )
    }
}